import java.util.Scanner;
import java.util.HashSet;
public class Testsssss {
    public static void main(String[] args) {
        Mainqqq mainqqq = new Mainqqq();
        mainqqq.mainE();
    }
}




// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Mainqqq {
    public static void mainE() {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int count = in.nextInt();//组数
            for (int i = 0; i < count; i++) {
                int x = in.nextInt();
                int y = in.nextInt();

                int _x = x;
                int n = 0;
                while(_x>=10){//判断数组长度

                    _x = _x/10;
                    n++;
                }
                n++;



                boolean flgg = true;
                for(int num = x; num<=y; num++){

                    HashSet<Integer> set = new HashSet<Integer>();
                    while(x>=10){
                        set.add(x%10);
                        x = x/10;
                    }
                    set.add(x);

                    boolean flg = true;
                    for(int j = 1;j<=n;j++){
                        if(!set.contains(j)){
                            flg = false;
                            break;
                        }
                    }
                    if(flg){
                        flgg = false;
                        System.out.println(num);
                    }
                }



                if(flgg)
                    System.out.println(-1);




            }
        }
    }

}